package utils;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class MyLogger 
{
	
	private PrintWriter pw = null;
	private static final String DATE_FORMAT_NOW = "yyyy-MM-dd HH:mm:ss";
	
	private String now() 
	{
	    Calendar cal = Calendar.getInstance();
	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
	    return sdf.format(cal.getTime());

	}
	
	public MyLogger(String logPath) throws Exception
	{
		int sep = logPath.lastIndexOf("\\");
	    
	    
		new File(logPath.substring(0, sep)).mkdirs();
		
		pw = new PrintWriter(new FileWriter(logPath, true));
	}
	
	public synchronized void AddEntry(String entry)
	{
		pw.println(now() + "\t" + entry);
		pw.flush();
	}
	
	public void Close()
	{
		pw.close();
	}
	
	
	public static void main(String[] args) throws Exception
	{
		MyLogger log = new MyLogger("D:\\development\\dblab\\log.log");
		log.AddEntry("c");
		log.AddEntry("d");
		log.Close();
	}
}
